﻿using System;
using System.Text;
using SLS.ExClassLib.MultiThreading.Extensions;

namespace SLS.ExClassLib.MultiThreading.Threading.ThreadPools.Statistics
{
    /// <summary>
    /// Represent extended thread pool statistics
    /// </summary>
    public class ExtendedThreadPoolStats
    {
        private const string DefualtExtendedThreadPoolName = "ExtendedThreadPool";

        public ExtendedThreadPoolStats(int taskItemsQueued, int taskItemsStarted, int consumersWaiting, int maxThreads)
        {
            TaskItemsQueued = taskItemsQueued;
            TaskItemsStarted = taskItemsStarted;
            ConsumersWaiting = consumersWaiting;
            MaxThreads = maxThreads;
        }

        public int TaskItemsQueued { get; private set; }
        public int TaskItemsStarted { get; private set; }
        public int ConsumersWaiting { get; private set; }
        public int MaxThreads { get; private set; }

        public string ToString(string threadPoolName)
        {
            return threadPoolName.IsNullOrEmpty() ? ToString() : DoToString(threadPoolName);
        }

        public override string ToString()
        {
            return DoToString(DefualtExtendedThreadPoolName);
        }

        private string DoToString(string threadPoolName)
        {
            var result = new StringBuilder("\n" + Environment.NewLine + threadPoolName + " state:");
            result.Append("\n");
            result.Append(Environment.NewLine);
            result.AppendFormat("\tQueued TaskItems\t=\t {0}", TaskItemsQueued);
            result.Append(Environment.NewLine);
            result.AppendFormat("\tStarted TaskItems\t=\t {0}", TaskItemsStarted);
            result.Append(Environment.NewLine);
            result.AppendFormat("\tWaiting Threads\t\t=\t {0}", ConsumersWaiting);
            result.Append(Environment.NewLine);
            result.AppendFormat("\tMax Threads\t\t=\t {0}", MaxThreads);
            result.Append(Environment.NewLine);
            return result.ToString();
        }
    }
}